package org.hxyjs.dao.impl;

import org.hxyjs.dao.ChatAdminDao;
import org.hxyjs.entity.ChatAdmin;
import org.hxyjs.mapper.ChatAdminMapper;
import org.hxyjs.page.PageInfo;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;

@Repository("chatadminDao")//一般实现类不会直接生成默认对象名 而是使用 接口的默认对象名
public class ChatAdminDaoImpl implements ChatAdminDao{
    @Resource //autowired 会装配能够直接扫描到的对象  resource spring容器运行的时候才找到需要的对象  注入
    private ChatAdminMapper chatadminMapper;

    public List<ChatAdmin>  getAllChatAdmins() throws Exception{ return chatadminMapper.selectByExample(null); }

    @Override
    public ChatAdmin getChatAdminByUno(String uno) throws Exception {
        return chatadminMapper.selectByPrimaryKey(uno); }

    @Override
    public List<ChatAdmin> getChatAdminsByPage(PageInfo pageInfo) {
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("currPage",pageInfo.getCurrPage());
        map.put("pageSize",pageInfo.getPageSize());
        int itemCount =(int) chatadminMapper.countByExample(null);
        pageInfo.setItemCount(itemCount);
        return chatadminMapper.getChatAdminsByPage(map);
    }

    @Override
    public Integer deleteChatAdmin(String uno) { return chatadminMapper.deleteByPrimaryKey(uno); }

    @Override
    public Integer updateChatAdmin(ChatAdmin chatadmin) { return chatadminMapper.updateByPrimaryKey(chatadmin); }



    @Override
    public ChatAdmin getChatAdminsWithOrders(String uno) throws Exception {
        return null;
    }

    @Override
    public ChatAdmin getAccountByAccountName(String uno) throws Exception {
        return null;
    }

    @Override
    public Integer addChatAdminByUno(ChatAdmin chatadmin){
        chatadmin.setCano(initId());
        return chatadminMapper.insertSelective(chatadmin);
    }
//
//    @Override
//    public ChatAdmin getChatAdminsWithOrders(String uno) throws Exception {
//        return chatadminMapper.getChatAdminsWithOrders(uno);
//    }
//    @Override
//    public ChatAdmin getAccountByAccountName(String uname) throws Exception {
//        return chatadminMapper.getAccountByAccountName(uname);
//    }
//
//
    public String initId() {
        String maxCano = chatadminMapper.getMaxCano();
        maxCano = Optional.ofNullable(maxCano).orElse("0000"); //如果maxUno 为空 就为0000
        return org.hxyjs.utils.GenNumTools.initId("u", 4, maxCano);
    }
}
